//
//  TBRDoPayOrderC.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/3.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRDoPayOrderC.h"
#import "TBRDoPayCell.h"
#import "TBRPrepayListModel.h"

#import "PaymentObject.h"
@interface TBRDoPayOrderC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;
@property (weak, nonatomic) IBOutlet UIButton *doPayBtn;

@end

@implementation TBRDoPayOrderC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
}
- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    //添加tableView
    [self setupTableView];
}
- (void)setupNavItem {
    self.titleText = @"支付款项";
}

- (void)setupTableView {
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRDoPayCell" bundle:nil] forCellReuseIdentifier:@"TBRDoPayCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -45, 0, 0)];
    }
    self.tableView.showsVerticalScrollIndicator = NO;
    
}
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TBRDoPayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TBRDoPayCellId" forIndexPath:indexPath];
    TBRDoPrepayModel *model = self.dataArray[indexPath.row];
    cell.row = indexPath.row;
    cell.model = model;
    __weak typeof(self) weakSelf = self;
    cell.changeSelBtnBlock = ^(NSInteger row) {
        [weakSelf.dataArray enumerateObjectsUsingBlock:^(TBRDoPrepayModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
            model.isSelect = @"0";
        }];
        TBRDoPrepayModel *model = weakSelf.dataArray[row];
        if ([model.isSelect isEqualToString:@"1"]) {
            model.isSelect = @"0";
        }else {
            model.isSelect = @"1";
        }
        
        NSInteger index = 0;
        for (TBRDoPrepayModel *model in weakSelf.dataArray) {
            if ([model.isSelect isEqualToString:@"1"]) {
                break;
            }
            index++;
        }
        if (index == 0) {
            [weakSelf.doPayBtn setTitle:@"微信支付:1000" forState:UIControlStateNormal];
        }else if (index == 1) {
            [weakSelf.doPayBtn setTitle:@"支付宝支付:1000" forState:UIControlStateNormal];
        }else if (index == 2) {
            [weakSelf.doPayBtn setTitle:@"银联支付:1000" forState:UIControlStateNormal];
        }else if (index == 3) {
            [weakSelf.doPayBtn setTitle:@"线下交易:1000" forState:UIControlStateNormal];
        }
        
        [weakSelf.tableView reloadData];
    };
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 58;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

// MARK: - 支付按钮点击------------
- (IBAction)daPayBtnClick:(UIButton *)sender {
    NSInteger index = 0;
    for (TBRDoPrepayModel *model in self.dataArray) {
        if ([model.isSelect isEqualToString:@"1"]) {
            break;
        }
        index++;
    }
    //根据index来选择支付方式---如果是0的话就是微信支付
    if (index == 0) {
        NSDictionary *content = @{@"appid": @"wxf111f0c2be2471ca",
                                  @"partnerid": @"1487942032",
                                  @"prepayid": @"wx04150119057818ab6285bc1d0824009133",
                                  @"package": @"Sign=WXPay",
                                  @"noncestr": @"3d771ec9c71e4172b0c38c08dd842577",
                                  @"timestamp": @"1525417279",
                                  @"sign": @"F5B75CF38AE23FCE8F2237B13257C120",
                                  @"attach": @"3|48801"};
        [[PaymentObject sharePayMange] wxPayAcion:content];
    }
    else if (index == 1) {
        [[PaymentObject sharePayMange] alipayAction:@"alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2017070807680886&biz_content=%7B%22body%22%3A%22%E5%95%86%E5%93%81%E6%94%AF%E4%BB%98%22%2C%22out_trade_no%22%3A%22B478D5B1D7015F989921%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%E5%95%86%E5%93%81%E6%94%AF%E4%BB%98%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%2245.90%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fisgstore.tnbchina.com%2Fapi%2Fpayment%2Falipay%2Fnotify_url.aspx&sign=wEjmexX8HJcp1cp40nvocrr4DAxF7b0rkwAVCINtyy0In0fpqAJiYyKlL6UTxAbs7f7lvOLIpulkAtmLNSeFRXZtuJViBtF%2B6kIphBAm%2BjiwgYGE%2F55oovNH0rCkEKq6%2BBAyL4rW1euCZPvfrtIDqK2XVuPqCanH1JnjxH2s35KsfbLHmPk0UgRofvDjMmNXDZ7FLL1mN%2Fhfr4%2BuEy8WUDl%2Fe8%2BnBdMLNhKDIDLWvWar4%2FG93j%2F273REBbHFox3B4nlw5cLL5HFYFi7zWY0Wv9FVCiFQ8ZGin0BLaRRMy14Tt%2BMOxPE8j81uapEYGftVyEAOly8JagK89MldgnzTlA%3D%3D&sign_type=RSA2&timestamp=2018-05-04+15%3A52%3A15&version=1.0"];
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadNetData];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    NSArray *tmpArray = @[
                          @{@"iconImg":@"",
                            @"titleStr":@"微信支付",
                            @"detailTitleStr":@"微信安全支付",
                            @"isSelect":@"1"},
                          @{@"iconImg":@"",
                            @"titleStr":@"支付宝支付",
                            @"detailTitleStr":@"支付宝安全支付",
                            @"isSelect":@"0"},
                          @{@"iconImg":@"",
                            @"titleStr":@"银联支付",
                            @"detailTitleStr":@"中国银联在线支付服务",
                            @"isSelect":@"0"},
                          @{@"iconImg":@"",
                            @"titleStr":@"线下交易",
                            @"detailTitleStr":@"线下汇款方式",
                            @"isSelect":@"0"},
                       ];
    self.dataArray = [NSArray yy_modelArrayWithClass:[TBRDoPrepayModel class] json:tmpArray];
    [self.tableView reloadData];
}
@end
